//
//  CommentDetailCell.m
//  FreeTime
//
//  Created by  on 16/9/5.
//  Copyright © 2016年 SRDZ.com. All rights reserved.
//

#import "CommentDetailCell.h"
#import "Header.h"

@interface CommentDetailCell (){
    UIButton * iconBtn,*commentBtn,*likeBtn;
    UILabel * nameLbl,*timeLbl,*contentLbl;
    NSIndexPath * indexPathCell;
    NSMutableArray * imgBtnArr;
}

@end

@implementation CommentDetailCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        iconBtn = [[UIButton alloc] init];
        iconBtn.backgroundColor = DEBUG_CLOLR;
        iconBtn.tag = 202;
        [iconBtn addTarget:self action:@selector(commentAndLikeBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:iconBtn];
        
        self.imgView = [[UIView alloc] init];
        //        _imgView.backgroundColor = [UIColor redColor];
        [self addSubview:_imgView];
        
        self.commentView = [[UIView alloc] init];
        //        _commentView.backgroundColor = [UIColor redColor];
        [self addSubview:_commentView];
        
        commentBtn = [[UIButton alloc] init];
        commentBtn.layer.borderColor = [ImageUtil colorWithRgb:0xebebeb].CGColor;
        commentBtn.layer.borderWidth = 1;
        [commentBtn setTitleColor:[ImageUtil colorWithRgb:0x999999] forState:UIControlStateNormal];
        commentBtn.tag = 200;
        [commentBtn addTarget:self action:@selector(commentAndLikeBtn:) forControlEvents:UIControlEventTouchUpInside];
        commentBtn.layer.cornerRadius = 5;
        commentBtn.titleLabel.font = FONT(13);
        [self addSubview:commentBtn];
        
        likeBtn = [[UIButton alloc] init];
        likeBtn.layer.borderColor = [ImageUtil colorWithRgb:0xebebeb].CGColor;
        likeBtn.layer.borderWidth = 1;
        [likeBtn addTarget:self action:@selector(commentAndLikeBtn:) forControlEvents:UIControlEventTouchUpInside];
        likeBtn.tag = 201;
        likeBtn.layer.cornerRadius = 5;
        likeBtn.titleLabel.font = FONT(13);
        [likeBtn setTitleColor:[ImageUtil colorWithRgb:0x999999] forState:UIControlStateNormal];
        [self addSubview:likeBtn];
        
        nameLbl = [[UILabel alloc] init];
        nameLbl.font = [UIFont boldSystemFontOfSize:14];
        nameLbl.textColor = [UIColor blackColor];
        [self addSubview:nameLbl];
        
        timeLbl = [UILabel new];
        timeLbl.font = FONT(14);
        timeLbl.textAlignment = NSTextAlignmentRight;
        timeLbl.textColor = [ImageUtil colorWithRgb:0x999999];
        [self addSubview:timeLbl];
        
        contentLbl = [UILabel new];
        contentLbl.font = FONT(14);
        contentLbl.numberOfLines = 0;
        contentLbl.textColor = [ImageUtil colorWithRgb:0x666666];
        [self addSubview:contentLbl];
    }
    return self;
}

- (void)bindData:(NSDictionary *)dic indexPath:(NSIndexPath *)indexPath{
    iconBtn.frame = [dic[@"iconFrame"] CGRectValue];
    nameLbl.frame = [dic[@"nameFrame"] CGRectValue];
    timeLbl.frame = [dic[@"timeFrame"] CGRectValue];
    contentLbl.frame = [dic[@"contentFrame"] CGRectValue];
    commentBtn.frame = [dic[@"commentFrame"] CGRectValue];
    likeBtn.frame = [dic[@"likeFrame"] CGRectValue];
    [iconBtn setBackgroundImage:[UIImage imageNamed:dic[@"iconImg"]] forState:UIControlStateNormal];
    iconBtn.layer.cornerRadius = 25;
    iconBtn.layer.masksToBounds = YES;
    nameLbl.text = dic[@"name"];
    timeLbl.text = dic[@"time"];
    contentLbl.text = dic[@"content"];
    [commentBtn setTitle:@"私密评论" forState:UIControlStateNormal];
    [likeBtn setTitle:@"喜欢" forState:UIControlStateNormal];
    self.imgView.frame = [dic[@"imgViewFrame"] CGRectValue];
    self.imgView.backgroundColor = [UIColor brownColor];
    for (UIView * view  in dic[@"commentArr"]) {
        [self.commentView addSubview:view];
    }
    
}
+ (NSMutableDictionary *)cellFrame:(NSMutableDictionary *)dic indexPath:(NSIndexPath *)indexPath{
    dic = [CommentDetailCell turn:dic indexPath:indexPath];
    
    CGRect iconFrame = CGRectMake(10, 20, 50, 50);
    CGFloat w = [ToolUtil autoWidth:dic[@"name"] width:0 height:15 num:14];
    CGRect nameFrame = CGRectMake(CGRectGetMaxX(iconFrame)+10, iconFrame.origin.y+5, w+10, 15);
    CGFloat y = [ToolUtil customAutoHeigh:dic[@"content"] width:WindowWidth-nameFrame.origin.x-70 num:14];
    
    CGRect contentFrame = CGRectMake(nameFrame.origin.x, CGRectGetMaxY(iconFrame)-13, WindowWidth-nameFrame.origin.x-70, y);
    CGFloat w1 = [ToolUtil autoWidth:dic[@"time"] width:0 height:10 num:14];
    CGRect timeFrame = CGRectMake(nameFrame.origin.x, CGRectGetMaxY(contentFrame)+20, w1, 10);
    CGRect commentFrame = CGRectMake(nameFrame.origin.x, CGRectGetMaxY(timeFrame)+10, 90, 25);
    CGRect likeFrame = CGRectMake(CGRectGetMaxX(commentFrame)+10, CGRectGetMaxY(timeFrame)+10, 60, 25);
    CGRect imageFrame = CGRectMake(WindowWidth-70, 10, 60, 60);
    
    NSDictionary * dicti =  [CommentDetailCell loadCommentView:dic withFrame:likeFrame];
    CGFloat y1 = CGRectGetMaxY(likeFrame)+10+[dicti[@"viewHeight"] intValue];
    
    CGFloat cellHeight = y1;
    [dic setObject:dicti[@"commentArr"] forKey:@"commentArr"];
    [dic setObject:[NSValue valueWithCGRect:imageFrame] forKey:@"imgViewFrame"];
    [dic setObject:[NSValue valueWithCGRect:iconFrame] forKey:@"iconFrame"];
    [dic setObject:[NSValue valueWithCGRect:nameFrame] forKey:@"nameFrame"];
    [dic setObject:[NSValue valueWithCGRect:timeFrame] forKey:@"timeFrame"];
    [dic setObject:[NSValue valueWithCGRect:contentFrame] forKey:@"contentFrame"];
    [dic setObject:[NSValue valueWithCGRect:commentFrame] forKey:@"commentFrame"];
    [dic setObject:[NSValue valueWithCGRect:likeFrame] forKey:@"likeFrame"];
    [dic setObject:[NSNumber numberWithFloat:cellHeight] forKey:@"cellHeight"];
    
    return dic;

}

+ (NSMutableDictionary*)turn:(NSMutableDictionary*)dict indexPath:(NSIndexPath *)indexPath{
    
    return [NSMutableDictionary dictionaryWithDictionary:dict];

}
+ (NSDictionary * )loadCommentView:(NSDictionary *)dict withFrame:(CGRect)frame{
    CGFloat y1 = 0;
    y1 = [dict[@"commentNum"] intValue] *50;
    NSMutableArray * commentViewArr = [NSMutableArray array];
    for (int i = 0; i< [dict[@"commentNum"] intValue]; i++) {
        UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(frame)+10+i*50, WindowWidth, 50)];
        bgView.backgroundColor = DEBUG_CLOLR;
        [ToolUtil createLine:49.5 superView:bgView lift:0 width:WindowWidth hight:0.5 color:[ImageUtil colorWithRgb:0xe6e6e6]];
        [commentViewArr addObject:bgView];
    }
    return @{@"commentArr":commentViewArr,@"viewHeight":[NSNumber numberWithFloat:y1]};
}


@end
